import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { message } from 'ant-design-vue'
import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '../pages/HomePage.vue'
import UserLoginPage from '../pages/user/UserLoginPage.vue'
import UserProfilePage from '../pages/user/UserProfilePage.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 主页
    {
      path: '/',
      name: '主页',
      component: HomePage,
    },
    // 用户登录
    {
      path: '/user/login',
      name: '用户登录',
      component: UserLoginPage,
    },
    // 用户注册
    {
      path: '/user/register',
      name: '用户注册',
      component: () => import('../pages/user/UserRegisterPage.vue'),
    },
    // 用户管理
    {
      path: '/admin/manage',
      name: '用户管理',
      component: () => import('../pages/admin/UserManagePage.vue'),
    },
    // 重置密码
    {
      path: '/user/resetPassword',
      name: '重置密码',
      component: () => import('../pages/user/ResetPassword.vue'),
    },
    // 个人中心
    {
      path: '/user/profile',
      name: '个人中心',
      component: UserProfilePage,
    },
    // 个人空间
    {
      path: '/user/space',
      alias: '/user/personalSpace',
      name: '个人空间',
      component: () => import('../pages/user/PersonalSpacePage.vue'),
      redirect: '/user/space/overview',
      children: [
        {
          path: 'overview',
          name: '空间预览',
          component: () => import('../pages/user/space/Overview.vue'),
        },
        {
          path: 'files',
          name: '我的文件',
          component: () => import('../pages/user/space/Files.vue'),
        },
        {
          path: 'pictures',
          name: '我的图片',
          component: () => import('../pages/user/space/Pictures.vue'),
        },
        {
          path: 'shares',
          name: '我的分享',
          component: () => import('../pages/user/space/Shares.vue'),
        },
        {
          path: 'favorites',
          name: '我的收藏',
          component: () => import('../pages/user/space/PersonalFavorites.vue'),
        },
        {
          path: 'likes',
          name: '我的点赞',
          component: () => import('@/pages/user/space/PersonalLikes.vue'),
        },
        {
          path: 'recycle',
          name: '回收文件',
          component: () => import('../pages/user/space/Recycle.vue'),
        },
      ],
    },
    // 测试页面
    {
      path: '/test',
      name: 'TestPage',
      component: () => import('../pages/TestPage.vue'),
    },
    // 用户管理
    {
      path: '/admin/userManage',
      name: '用户管理',
      component: () => import('../pages/admin/UserManagePage.vue'),
      meta: {
        requiresAdmin: true,
      },
    },
    // 用户图片列表
    {
      path: '/user/pictureList',
      name: '用户图片列表',
      component: () => import('../pages/user/PersonalSpacePage.vue'),
    },
    // 资源图片
    {
      path: '/admin/unsplashPic',
      name: '资源图片',
      component: () => import('../pages/admin/UnsplashPic.vue'),
      meta: {
        requiresAdmin: true,
      },
    },
    // 图片采集
    {
      path: '/admin/imageCrawler',
      name: '图片采集',
      component: () => import('../pages/admin/ImageCrawler.vue'),
      meta: {
        requiresAdmin: true,
      },
    },
    // 创建资源
    {
      path: '/user/createSource',
      name: '创建资源',
      component: () => import('../pages/user/CreateSource.vue'),
      redirect: '/user/createSource/image-upload',
      children: [
        {
          path: 'image-upload',
          name: '图片上传',
          component: () => import('../pages/user/create/ImageUpload.vue'),
        },
        {
          path: 'video-upload',
          name: '视频上传',
          component: () => import('../pages/user/create/VideoUpload.vue'),
        },
        {
          path: 'upload-history',
          name: '上传历史',
          component: () => import('../pages/user/create/UploadHistory.vue'),
        },
      ],
    },
    // 图片管理
    {
      path: '/admin/pictureManage',
      name: '图片管理',
      component: () => import('../pages/admin/PictureManage.vue'),
      meta: {
        requiresAdmin: true,
      },
    },
    // 团队空间
    {
      path: '/team/space',
      name: 'TeamSpace',
      component: () => import('../pages/user/TeamSpacePage.vue'),
      children: [
        {
          path: ':spaceId',
          name: 'TeamSpaceDetail',
          component: () => import('@/pages/user/TeamSpaceDetail.vue'),
          meta: {
            title: '空间详情',
            requiresAuth: true,
          },
        },
      ],
    },
    // 智能天堂
    {
      path: '/intelligent/paradise',
      name: '智能天堂',
      component: () => import('@/pages/user/IntelligentParadisePage.vue'),
      redirect: '/intelligent/paradise/overview',
      children: [
        {
          path: 'overview',
          name: '智能总览',
          component: () => import('@/pages/user/intelligent/IntelligentOverview.vue'),
        },
        {
          path: 'ai-tools',
          name: 'AI工具箱',
          component: () => import('@/pages/user/intelligent/IntelligentAITools.vue'),
        },
        {
          path: 'ai-history',
          name: '智能历史',
          component: () => import('@/pages/user/intelligent/IntelligentAIHistory.vue'),
        },
        {
          path: 'ai-chat',
          name: '智能客服',
          component: () => import('@/pages/user/intelligent/IntelligentAIChat.vue'),
        },
        {
          path: 'ai-assistant',
          name: '智能助手',
          component: () => import('@/pages/user/intelligent/IntelligentAssistant.vue'),
        },
      ],
    },
    // 分享页面路由
    {
      path: '/s/:id',
      name: 'SharePage',
      component: () => import('@/pages/share/SharePage.vue'),
      meta: {
        title: '文件分享',
        requireAuth: false,
        layout: 'blank',
      },
    },
    // 用户反馈管理
    {
      path: '/feedback-manage',
      name: 'FeedbackManage',
      component: () => import('@/pages/feedback/FeedbackManagePage.vue'),
      meta: {
        requiresAuth: true,
        requiresAdmin: true, // 仅管理员可访问
      },
    },
    // 系统监控
    {
      path: '/admin/monitor',
      name: '系统监控',
      component: () => import('@/pages/admin/monitor/MonitorLayout.vue'),
      meta: { requiresAdmin: true },
      children: [
        {
          path: '',
          name: '监控总览',
          component: () => import('@/pages/admin/monitor/MonitorDashboard.vue'),
        },
        {
          path: 'redis',
          name: 'Redis监控',
          component: () => import('@/pages/admin/monitor/RedisMonitor.vue'),
        },
        {
          path: 'mysql',
          name: 'MySQL监控',
          component: () => import('@/pages/admin/monitor/MysqlMonitor.vue'),
        },
        {
          path: 'cos',
          name: 'COS监控',
          component: () => import('@/pages/admin/monitor/CosMonitor.vue'),
        },
      ],
    },
  ],
})

// 添加全局路由守卫
router.beforeEach(async (to, from, next) => {
  const loginUserStore = useLoginUserStore()
  // 如果用户信息还没加载，先加载
  if (!loginUserStore.loginUser || !loginUserStore.loginUser.userRole) {
    if (typeof loginUserStore.fetchLoginUser === 'function') {
      await loginUserStore.fetchLoginUser()
    }
  }
  // 如果路由需要管理员权限
  if (to.meta.requiresAdmin) {
    // 检查用户是否是管理员
    if (loginUserStore.loginUser?.userRole === 'admin') {
      next()
    } else {
      // 不是管理员则跳转到首页
      next('/')
      message.error('无权访问该页面')
    }
  } else {
    next()
  }
})

export default router
